import java.util.Arrays;
import java.util.List;

import javax.swing.tree.TreeNode;

/*
 * @lc app=leetcode.cn id=257 lang=java
 *
 * [257] 二叉树的所有路径
 *
 * https://leetcode-cn.com/problems/binary-tree-paths/description/
 *
 * algorithms
 * Easy (66.78%)
 * Likes:    471
 * Dislikes: 0
 * Total Accepted:    103.2K
 * Total Submissions: 154.6K
 * Testcase Example:  '[1,2,3,null,5]'
 *
 * 给定一个二叉树，返回所有从根节点到叶子节点的路径。
 * 
 * 说明: 叶子节点是指没有子节点的节点。
 * 
 * 示例:
 * 
 * 输入:
 * 
 * ⁠  1
 * ⁠/   \
 * 2     3
 * ⁠\
 * ⁠ 5
 * 
 * 输出: ["1->2->5", "1->3"]
 * 
 * 解释: 所有根节点到叶子节点的路径为: 1->2->5, 1->3
 * 
 */

// @lc code=start
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    public List<String> binaryTreePaths(TreeNode root) {
        if(root == null) return Arrays.asList();
        List<String> list = new ArrayList();
        deep(root,String.valueOf(root.val),list);
        return list;
    }

    private void deep(TreeNode node,String tmp,List<String> list){
        if(node.left ==null && node.right ==null) list.add(tmp);
        if(node.left !=null){
            StringBuffer buffer = new StringBuffer(tmp);
            buffer.append("->").append(node.left.val);
            deep(node.left, buffer.toString(), list);
        }
        if(node.right !=null){
            StringBuffer buffer = new StringBuffer(tmp);
            buffer.append("->").append(node.right.val);
            deep(node.right, buffer.toString(), list);
        }
    }

}
// @lc code=end

